package org.catchmenow.play;


import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
	
public class Server implements ServerAPI 
{
	private GameStatus gameStatus;
	private boolean joinGameFirstRequestDone;
	private boolean takeRequests;
	
    public Server(int gridSize, int noOfTreasures) 
    {
    	gameStatus = new GameStatus(gridSize, noOfTreasures);
    	joinGameFirstRequestDone = false;
    	takeRequests = true;
    }
    
    public boolean getJoinGameFirstRequestDone()
    {
    	return getsetJoinGameFirstRequestDone(true, false);
    }
    
    public void setJoinGameFirstRequestDone(boolean set)
    {
    	getsetJoinGameFirstRequestDone(set, true);
    }
    
	private synchronized boolean getsetJoinGameFirstRequestDone(
			boolean joinGameFirstRequestDone, boolean isSet) {
		if (isSet)
		{
			this.joinGameFirstRequestDone = joinGameFirstRequestDone;
		}
		else
		{
			return this.joinGameFirstRequestDone;
		}
		return false;
	}
	
	public boolean getTakeRequests()
	{
		return getsetTakeRequests(true, false);
	}
	
	public void setTakeRequests(boolean set)
	{
		getsetTakeRequests(set, true);
	}
	
	private synchronized boolean getsetTakeRequests(boolean takeRequests, boolean isSet) {
		if (isSet)
		{
			this.takeRequests = takeRequests;
		}
		else
		{
			return this.takeRequests;
		}
		return false;
	}

	public String sayHello() {
	System.out.println("Client connected");

	
	
/*	try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
*/
	
	
	return "Hello, world!";
    }
	
    public static void start(int gridSize, int noOfTreasures) 
    {
    	ServerAPI stub = null;
    	Registry registry = null;
	
    	try 
    	{
    		Server obj = new Server(gridSize, noOfTreasures);
    		stub = (ServerAPI) UnicastRemoteObject.exportObject(obj, 0);
    		registry = LocateRegistry.getRegistry();
    		registry.bind("Hello", stub);

    		System.err.println("Server ready");
    	} 
    	catch (Exception e) 
    	{
    		try
    		{
    			registry.unbind("Hello");
    			registry.bind("Hello",stub);
    			System.err.println("Server ready");
    		}
    		catch(Exception ee)
    		{
    			System.err.println("Server exception: " + ee.toString());
    			ee.printStackTrace();
    		}
    	}
    }
    
	@Override
	public StartGameStatus joinGame() throws RemoteException 
	{
		StartGameStatus sgs = null;
		if (getTakeRequests())
		{
			sgs = new StartGameStatus();
		}
		else
		{
			setTakeRequests(true);
			int id = gameStatus.addPlayer();
			sgs = new StartGameStatus(true, gameStatus, id);
			while (takeRequests)
			{
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					System.out.println("Error in waiting in joinGame");
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
		}
		return sgs;
	}
}
